﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AutoUpdate.DAL;

namespace AutoUpdate.BLL
{
    public abstract class BaseManager<T> where  T: class,new ()
    {
        public abstract void SetDal();

        public BaseManager()
        {
            SetDal();
        }

        public BaseDAL<T> Dal { get; set; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Add(T t)
        {
            Dal.Add(t);
            return Dal.SaveChanges();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Update(T t)
        {
            Dal.Update(t);
            return Dal.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Dleete(T t)
        {
            Dal.Delete(t);
            return Dal.SaveChanges();
        }

        /// <summary>
        /// 根据条件返回
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> GetModels(Expression<Func<T,bool>> whereLambda)
        {
            return Dal.GetModel(whereLambda);
        }

        /// <summary>
        /// 根据条件分页返回
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="isAsc"></param>
        /// <param name="OrderByLambda"></param>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> GetModelsByPage<type>(int pageSize, int pageIndex, bool isAsc,
            Expression<Func<T, type>> OrderByLambda, Expression<Func<T, bool>> whereLambda)
        {
            return Dal.GetModelByPage(pageSize, pageIndex, isAsc, OrderByLambda, whereLambda);
        }

    }
}
